#****************************************************************************
# target output
#****************************************************************************
OUTPUT      := example.bin

#****************************************************************************
# DEBUG can be set to YES to include debugging info, or NO otherwise
#****************************************************************************
DEBUG       := YES

#****************************************************************************
# PROFILE can be set to YES to include profiling info, or NO otherwise
#****************************************************************************
PROFILE     := NO

#****************************************************************************
# add your compile macro, such as: -DDEBUG_LEVEL=0
#****************************************************************************
DEFS        :=
CFLAGS      := ${DEFS}

#****************************************************************************
# add your source & header file path
#****************************************************************************
ROOT_DIR    := $(abspath ${CURDIR}/../)
PROJ_DIR    := ${ROOT_DIR}/example
INC_DIRS    := -I${ROOT_DIR}
INC_DIRS    +=
SRC_DIRS    := ${ROOT_DIR}
SRC_DIRS    += ${ROOT_DIR}/example

#****************************************************************************
# set your toolchain prefix
#****************************************************************************
TOOLCHAIN_PREFIX    :=
CC                  := $(TOOLCHAIN_PREFIX)gcc
LD                  := $(TOOLCHAIN_PREFIX)gcc
AR                  := $(TOOLCHAIN_PREFIX)ar

#****************************************************************************
# set your compile parameters
#****************************************************************************
DEBUG_CFLAGS    := -Wall -Wno-format -MMD -DDEBUG -std=gnu99
RELEASE_CFLAGS  := -Wall -Wno-unknown-pragmas -Wno-format -MMD -O3 -std=gnu99
LIBS            := -lpthread -lrt

DEBUG_LDFLAGS   := -g
RELEASE_LDFLAGS :=

ifeq (YES, ${DEBUG})
  CFLAGS    += ${DEBUG_CFLAGS}
  LDFLAGS   := ${DEBUG_LDFLAGS}
else
  CFLAGS    += ${RELEASE_CFLAGS}
  LDFLAGS   := ${RELEASE_LDFLAGS}
endif

ifeq (YES, ${PROFILE})
  CFLAGS    += -pg -O3
  LDFLAGS   += -pg
endif

#****************************************************************************
# lib files path
#****************************************************************************
LIB_DIRS    :=
EXTRA_LIBS  := $(foreach dir, $(LIB_DIRS), $(wildcard $(dir)/*.a))

#****************************************************************************
# start prepare operation
#****************************************************************************
BUILD_DIR   := ${PROJ_DIR}/build
SRC_FILES   := $(foreach dir, $(SRC_DIRS), $(wildcard $(dir)/*.c))
OBJ_FILES   := $(SRC_FILES:%.c=$(BUILD_DIR)/%.o)
DEP_FILES   := $(OBJ_FILES:%.o=%.d)

#****************************************************************************
# start compile operation
#****************************************************************************
${OUTPUT} : $(BUILD_DIR)/$(OUTPUT)
${BUILD_DIR}/${OUTPUT} : ${OBJ_FILES}
	@echo "Build $(notdir $@)..."
	@-mkdir -p ${@D}
	@$(CC) $^ ${CFLAGS} ${INC_DIRS} ${LDFLAGS} ${LIBS} ${EXTRA_LIBS} -o $@
-include ${DEP_FILES}

.PHONY : all clean distclean execute
all:
${BUILD_DIR}/%.o : %.c
	@echo "Build $(notdir $<)..."
	@-mkdir -p $(@D)
	@$(CC) $(CFLAGS) ${INC_DIRS} ${LDFLAGS} ${LIBS} ${EXTRA_LIBS} -c $< -o $@

clean:
	-rm -rf $(BUILD_DIR)

distclean:
	-rm -rf ${BUILD_DIR} ./${OUTPUT}

execute :
	-cp ${BUILD_DIR}/${OUTPUT} ./
	./${OUTPUT}